<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台优惠券ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

quanli_require('module/base/content/goodsComment.class.php');

class ajax_goodsComment {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品评论
                    $this->searchGoodsComment();
                    break;

                case 2: //查询订单商品评论
                    $this->searchBillGoodsComment();
                    break;

                case 3: //提交商品评论
                    $this->submitGoodsComment();
                    break;

                case 4: //根据评论选项查询评论
                    $this->searchGoodsCommentForItem();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }

    private function searchGoodsComment() {

        global $G_SHOP;

        if (!isset($_REQUEST['twoPostID']) || !quanli_is_int($_REQUEST['twoPostID'])) {
            returnJsonData('参数错误');
        }

        if (!isset($_REQUEST['page']) || !quanli_is_int($_REQUEST['page'])) {
            $_REQUEST['page'] = 1;
        }

        $twoPostID = $_REQUEST['twoPostID'];

        $page = $_REQUEST['page'];

        $item = isset($_REQUEST['item'])?intval($_REQUEST['item']):0;

        $redis = &$G_SHOP->memoryCache_Instance;

        //读取数据缓存
        $result = $redis->zRange('goods_comment_'.$twoPostID.'_'.$item,$page-1,$page-1);

        if($result)

            returnJsonData('cache', 200, unserialize($result[0]));

        //实例商品评论类
        $goodsComment = new goodsComment();

        //查询商品评论
        $args_array = array(
            'twoPostID' => $twoPostID,
            'comment_status' => 1,
            'extend' => '',
            'page' => $page,
            'rp' => 5,
            'sortname' => 'comment_id',
            'sortorder' => 'DESC'
        );

        if ($G_SHOP->currentPCustID) {
            $args_array['extend'] = ' AND (display = 1 OR (pcustID = ' . $G_SHOP->currentPCustID . ' AND display = 0))';
        } else {
            $args_array['extend'] = ' AND display = 1';
        }

        $goods_comment_array = $goodsComment->searchComments( $args_array );

        if ($goods_comment_array) {

            foreach($goods_comment_array as &$comment) {
                if (is_mobile($comment['creator'])) {
                    $comment['creator'] = hide_mobile( $comment['creator'] );
                } else if (is_email($comment['creator'])) {
                    $comment['creator'] = hide_email( $comment['creator'] );
                }
            }

            $result_array = array(
                'comment' => $goods_comment_array,
                'paging' => shop_page($args_array['page'], $args_array['count'], $args_array['rp'], 'javascript: G.goods.comment(%d);', $G_SHOP->smarty)
            );

            //保存数据缓存
            $redis->zAdd('goods_comment_'.$twoPostID.'_'.$item,$page,serialize($result_array));

            returnJsonData('ok', 200, $result_array);

        } else {
            returnJsonData('商品评论没有找到', 404);
        }

    }

    /**
     * 查询订单商品评论
     */
    private function searchBillGoodsComment() {

        global $G_SHOP;

        //检查是否登录
        if (!$G_SHOP->currentPCustID) {
            returnJsonData('请先登录', 404);
        }

        if (!isset($_REQUEST['bill_no']) || empty($_REQUEST['bill_no'])) {
            returnJsonData('缺少参数', 400);
        }

        //实例商品评论类
        $goodsComment = new goodsComment();

        //查询订单商品评论
        $args_array = array(
            'bill_no' => $_REQUEST['bill_no'],
            'pcustID' => $G_SHOP->currentPCustID
        );
        $comment_array = $goodsComment->searchComments( $args_array );
        if ($comment_array) {
            returnJsonData('ok', 200, $comment_array);
        } else {
            returnJsonData('订单商品评论没有找到', 404);
        }

    }

    /**
     * 提交商品评论
     */
    private function submitGoodsComment() {

        global $G_SHOP;

        //检查是否登录
        if (!$G_SHOP->currentPCustID) {
            returnJsonData('请先登录', 404);
        }

        //实例商品评论类
        $goodsComment = new goodsComment();

        //保存商品评论
        $_REQUEST['operator'] = $G_SHOP->currentCustomerID;
        $_REQUEST['pcustID'] = $G_SHOP->currentPCustID;
        $result_array = $goodsComment->saveGoodsComments( $_REQUEST );

        returnJsonData($result_array['message'], $result_array['code']);


    }

    private function searchGoodsCommentForItem() {

        global $G_SHOP;

        quanli_include('module/base/customer/quanliCust.class.php');
        quanli_include('module/base/goods/quanliGoods.class.php');

        //实例会员类
        $quanliCust = new quanliCust();

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        if (!isset($_REQUEST['twoPostID']) || !quanli_is_int($_REQUEST['twoPostID'])) {
            returnJsonData('参数错误');
        }

        if (!isset($_REQUEST['item']) || !in_array($_REQUEST['item'], array(1, 2, 3, 4, 5))) {
            returnJsonData('参数错误');
        }

        if (!isset($_REQUEST['page']) || !quanli_is_int($_REQUEST['page'])) {
            $_REQUEST['page'] = 1;
        }

        $goodsID = intval($_REQUEST['twoPostID']);

        $page = intval($_REQUEST['page']);

        $itemID = intval($_REQUEST['item']);

        $redis = &$G_SHOP->memoryCache_Instance;

        //读取数据缓存
        $result = $redis->zRange('goods_comment_'.$goodsID.'_'.$itemID,$page-1,$page-1);

        if($result)

            returnJsonData('cache', 200, unserialize($result[0]));

        $rp = 5;

        $sql = ' WHERE twoPostID = ' . $_REQUEST['twoPostID'] . ' AND comment_status = 1 ';
        if ($G_SHOP->currentPCustID) {
            $sql .= ' AND (display = 1 OR (pcustID = ' . $G_SHOP->currentPCustID . ' AND display = 0))';
        } else {
            $sql .= ' AND display = 1';
        }

        //统计数量
        $search_sql = 'SELECT COUNT(*) AS item_count FROM content_comments cc
                       INNER JOIN content_comments_item cct ON cct.comment_id = cc.comment_id AND cct.comment_item = ' . $_REQUEST['item'];
        $commnet_count_array =& $G_SHOP->DBCA->stmt_query($search_sql . $sql);
        if ($commnet_count_array[0]->item_count == 0) {
            returnJsonData('商品评论没有找到', 404);
        }
        $commnet_count = $commnet_count_array[0]->item_count;

        //查询评论
        $sql .= ' ORDER BY cc.comment_id DESC';
        $sql .= sprintf(' LIMIT %d, %d', ($_REQUEST['page'] - 1) * $rp, $rp);
        $search_sql = 'SELECT cc.* FROM content_comments cc
                       INNER JOIN content_comments_item cct ON cct.comment_id = cc.comment_id AND cct.comment_item = ' . $_REQUEST['item'];
        $commnet_count_array =& $G_SHOP->DBCA->stmt_query($search_sql . $sql);
        if ($commnet_count_array) {

            $item_array = array();
            foreach($commnet_count_array as &$item) {

                //查询商品信息
                $goods_args_array = array(
                    'postID' => $item->postID
                );
                $goods_array = $quanliGoods->searchGoodsSaleDetail( $goods_args_array );
                if (!$goods_array) {
                    continue;
                }

                if (is_mobile($item->creator)) {
                    $item->creator = hide_mobile($item->creator);
                } else if (is_email($item->creator)) {
                    $item->creator = hide_email($item->creator);
                }

                $item_array[] = array(
                    'creator' => $item->creator,
                    'comment_id' => $item->comment_id,
                    'postID' => $item->postID,
                    'pcustID' => $item->pcustID,
                    'goodsName' => $item->goodsName,
                    'img_link' => $goods_array['img_link'],
                    'link' => $goods_array['link'],
                    'wap_link' => $goods_array['wap_link'],
                    'goods_spec' => $goods_array['spec'],
                    'createTime' => $item->createTime,
                    'comment_content' => $item->comment_content,
                    'comment_status' => $item->comment_status,
                    'answer_content' => $item->answer_content,
                    'answer_userID' => $item->answer_userID,
                    'answer_dateTime' => $item->answer_dateTime,
                    'satisfaction' => $item->satisfaction,
                    'display' => $item->display,
                    'recommend' => $item->recommend
                );

            }

            $result_array = array(
                'comment' => $item_array,
                'paging' => shop_page($_REQUEST['page'], $commnet_count, $rp, 'javascript: G.goods.comment(' . $_REQUEST['item'] . ',%d);', $G_SHOP->smarty)
            );

            //保存数据缓存
            $redis->zAdd('goods_comment_'.$goodsID.'_'.$itemID,$page,serialize($result_array));

            returnJsonData('ok', 200, $result_array);

        } else {
            returnJsonData('商品评论没有找到', 404);
        }

    }

}